package org.as3lib.math
{
	/**
	 * A class for doing rough comparisons of numbers.
	 * 
	 * @author Mims H. Wright
	 */
	public class FuzzyMath
	{
		/**
		 * Returns true if the two numbers are within "marginOfError" percent of each other. 
		 * 
		 * @example <listing version='3.0'>
		 * FuzzyMath.roughlyEqual(0.7, 0.69); // true
		 * FuzzyMath.roughlyEqual(0.7, 0.5); // false
 		 * 
		 * FuzzyMath.roughlyEqual(123456789, 123450000); // true
		 * FuzzyMath.roughlyEqual(123456789, 100000000); // false
		 * FuzzyMath.roughlyEqual(123456789, 100000000, 0.25); // true
		 * 
		 * </listing>
		 */
		public static function roughlyEqual ( a:Number, b:Number, marginOfError:Number = 0.10 ):Boolean {
			return Math.abs((a/b) - 1.0) < marginOfError; 
		}
		
		/**
		 * Returns true if a number is between the high and low values.
		 * 
		 * @param x The number to evaluate
		 * @param low The low end to check
		 * @param high The high end to check
		 * @param inclusive If true, and x == low or x == high, will return true. 
		 */
		public static function isBetween (x:Number, low:Number, high:Number, inclusive:Boolean = true):Boolean {
			if (inclusive) return (x >= low && x <= high);
			return (x > low && x < high);
		}
	}
}